//
//  XYInputToolBar.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/21.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

enum XYInputToolBarType : Int {
    case  Picture = 0
    case  Mention = 1
    case  Trend = 2
    case  Emoticon = 3
    case  Add = 4
}

protocol XYInputToolBarDelegate : NSObjectProtocol{
    func didInputToolBarButton(toolbar:XYInputToolBar,type:XYInputToolBarType)
}

class XYInputToolBar: UIView {
    
    //定义一个代理属性
    var delegate : XYInputToolBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)

        setToolBarButton("compose_toolbar_picture",type: .Picture)
        setToolBarButton("compose_mentionbutton_background",type: .Mention)
        setToolBarButton("compose_trendbutton_background",type: .Trend)
        setToolBarButton("compose_emoticonbutton_background",type: .Emoticon)
        setToolBarButton("compose_add_background",type: .Add)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //添加按钮
    private func setToolBarButton(imageName:String,type:XYInputToolBarType){
        let button = UIButton()
        
        button.addTarget(self, action: "didChlidButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.tag = type.rawValue
        
        addSubview(button)
    }
    
    //按钮点击事件
    @objc private func didChlidButton(button:UIButton){
        
        if let del = delegate{
            del.didInputToolBarButton(self, type: XYInputToolBarType(rawValue: button.tag)!)
        }
        
    }
    
    override func layoutSubviews() {
         super.layoutSubviews()
        
        let childButtonW = self.width / CGFloat(subviews.count)
        
        for i in 0..<subviews.count {
            let button = subviews[i]
            button.width = childButtonW
            button.height = self.height
            button.x = CGFloat(i) * childButtonW
            
        }
        
        
    }
}
